package com.system.AttendanceCherkDepartment.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.system.AttendanceCherkDepartment.dao.AttendanceRepository;
import com.system.AttendanceCherkDepartment.entity.Attendance;
import com.system.AttendanceCherkDepartment.entity.AttendanceEvent;
import com.system.employee.dao.EmployeeRepository;
import com.system.employee.entity.Employee;
import com.system.employee.service.IEmployeeService;

@Service
@Transactional
public class AttendanceService implements IAttendanceService {
	@Autowired
	private AttendanceRepository attendanceRepository;
	
	@Autowired
	private EmployeeRepository employeeRepository;
	//1.增加、修改考勤信息（每个参数都要传）
    //2.休假次数大于4后无法休假
	public void saveOrUpdate(Attendance attendance) 
	{
		System.out.println(attendance);
		if(null!=attendance.getId()) {
			attendanceRepository.updateAttendance(attendance.getId(),attendance.getAttendanceEvent(),attendance.getTime(),attendance.getTotalAbsence(),attendance.getTotalOvertime(),attendance.getTotalLeave(),attendance.getTotalVacation(),attendance.getTotalscore());
		}else {
			attendanceRepository.save(attendance);
		}
		if(attendance.getTotalVacation()>4 && attendance.getAttendanceEvent()==AttendanceEvent.vacation )
		{
			attendanceRepository.delete(attendance);
			//System.out.println("休假失败(>4)");
		}
	}
	//删除考察信息
	public void delete(Attendance attendance) 
	{
		attendance.setEmployee(null);
		attendanceRepository.save(attendance);
		attendanceRepository.delete(attendance);
	}
	//按id查询考勤信息
	public Attendance findOne(Long id) {
		return attendanceRepository.findOne(id);
	}
	//查询所有考察信息
	public List<Attendance> findAll(){
		return (List<Attendance>) attendanceRepository.findAll();
	}
	
	//增加、修改考勤信息（每个参数都要传）
   	public int saveOrUpdate(Long id,AttendanceEvent attendanceEvent,Date time,Long totalAbsence,Long totalOvertime,Long totalLeave,Long totalVacation,Long totalscore) {
   		return attendanceRepository.updateAttendance(id,attendanceEvent,time,totalAbsence,totalOvertime,totalLeave,totalVacation,totalscore);
   	}
   	//按id删除考勤信息
	public int deleteAttendances(Long[] ids) {
		return attendanceRepository.deleteAttendances(ids);
	}
	
	//根据考勤事件类型查找，或者第一个参数传 "%%" 查找全部
	public Page<Attendance> findAll(AttendanceEvent attendanceEvent ,Pageable pageable){
		return attendanceRepository.findAll(attendanceEvent, pageable);
	}
	//根据员工ID查找考勤信息
	public Page<Attendance> findAll(Long empId, Pageable pageable){
		return attendanceRepository.findAll(empId, pageable);
	}

//显示attendance表及关联的employee1表中的员工empName
	public Page<Attendance> findAll(Pageable pageable) {
		
		return attendanceRepository.findAll(pageable);
	}
	@Override
	public ArrayList<Attendance> findAllByOwner() {	
		ArrayList<Attendance> listAll=attendanceRepository.findAll();
		//同一个人的数据合一条数据
		ArrayList<Attendance> listByOwner=new ArrayList<Attendance>();
		Employee empInit=new Employee();
		Attendance init=new Attendance();
		init.setEmployee(empInit);
		listByOwner.add(init);
		for(Attendance atd:listAll)
		{
			int count=0;
			for(Attendance i :listByOwner)
			{
				count++;
              if(atd.getEmployee().getEmpId()==i.getEmployee().getEmpId())
              {
        	    break;
              }
  			  if(count==listByOwner.size())
  				{
				  listByOwner.add(atd);
  			      break;
  				}
			}
		};
		listByOwner.remove(init);
		return listByOwner;
	}

	
	//根据事件和userid增加
	public void save(String attendanceEvent,String userid) {
		Long absenceCount=0L;
		Long leaveCount=0L;
		Long overtimeCount=0L;
		Long vacationCount=0L;
		Long score=0L;
		Attendance attendance=new Attendance();
		attendance.setTime(new Date());
		Attendance result = new Attendance();
		//找到所有employee
		List<Employee> emplist=employeeRepository.findAll();

		for(Employee emp:emplist)
		{
			
			if(emp.getUser().getUid()==userid)
			{
		         attendance.setEmployee(emp);
		             ArrayList<Attendance> atdList=attendanceRepository.findAll();
		                 for(Attendance atd:atdList)
		                 {
		                	 if(atd.getEmployee()==emp)
		                	 {
		                		 absenceCount=atd.getTotalAbsence();
		                		leaveCount=atd.getTotalLeave();
		                		overtimeCount=atd.getTotalOvertime();
		                		vacationCount=atd.getTotalVacation();
		                		break;
		                	 }
		                 }
		         break;
			}	
		};
	//////////	

	     if(attendanceEvent=="A")
	     {
	    attendance.setAttendanceEvent(AttendanceEvent.leave);
	    attendance.setTotalLeave(++leaveCount);
	    attendance.setTotalAbsence(absenceCount);
	    attendance.setTotalOvertime(overtimeCount);
	    attendance.setTotalVacation(vacationCount);}    
	    else if(attendanceEvent=="B")
	    {
	    	if(vacationCount>4) {
	    		attendance.setAttendanceEvent(AttendanceEvent.vacation);
	    		attendance.setTotalVacation(++vacationCount);
	    		attendance.setTotalAbsence(absenceCount);
		    	attendance.setTotalLeave(leaveCount);
		    	attendance.setTotalOvertime(overtimeCount);
	    	}
	    }
	     if(leaveCount>3)
		    	score=(long) ((-0.5*(absenceCount*2-3))+(leaveCount-3)*(-0.5)+overtimeCount);
		    else
		        score=(long) (-0.5*(absenceCount*2-3)+overtimeCount);
	     attendance.setTotalscore(score);
	     attendanceRepository.save(attendance);
	     ArrayList<Attendance> alist=attendanceRepository.findAll(attendance.getEmployee().getEmpId());
	     for (Attendance at :alist) {
				at.setTotalAbsence(absenceCount);
				at.setTotalLeave(leaveCount);
				at.setTotalOvertime(overtimeCount);
				at.setTotalVacation(vacationCount);
				at.setTotalscore(score);
				at.setSalary(score*100);
				attendanceRepository.save(at);
			}
		
	///////	
		
		
		
	}
	
	
	public int deleteAttendances(List<Long> lists) {
		List<Attendance> attendances =  (List<Attendance>) attendanceRepository.findAll(lists);
		if(attendances!=null) {
			attendanceRepository.delete(attendances);
		}
		return 0;
	}
	
	@Override
	public Attendance findOnebyEmp(Long empId) {
		ArrayList<Attendance> all=attendanceRepository.findAll(empId);
		Attendance FirstRecord=all.get(0);
      return null;
	}	
	
	
	
	
}
